package com.example.rabbitspringamqp.config;

import com.example.rabbitspringamqp.listener.delegate.MessageDelegate;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.ConsumerTagStrategy;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author haitao
 * @date 2021/7/25 4:48 下午
 * @description 消息监听配置
 */
@Configuration
public class SimpleMessageListenerContainerConfig {

    @Resource
    private Queue directQueue;
    @Resource
    private Queue topicQueue;
    @Resource
    private Queue fanoutQueue;
    @Resource
    private Queue headersQueue;

    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
        simpleMessageListenerContainer.setQueues(directQueue, topicQueue, headersQueue, fanoutQueue);
        //监听队列的并发数量，mq会初始化对应数量的channel
        //独占模式只能设置为1，并发数大于1时不保证顺序消费过程
        simpleMessageListenerContainer.setConcurrentConsumers(1);
        //设置消费者数量的上限； 默认为“concurrentConsumers”。 消费者将按需添加。 不能少于 concurrentConsumers。
        simpleMessageListenerContainer.setMaxConcurrentConsumers(3);
        //消息拒绝后重回队列，一般设置为false
        simpleMessageListenerContainer.setDefaultRequeueRejected(false);
        //签收模式，一般设置为自动签收
        simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        //是否开启事务
        simpleMessageListenerContainer.setChannelTransacted(false);
        //消费者标签
        simpleMessageListenerContainer.setConsumerTagStrategy(new ConsumerTagStrategy() {
            @Override
            public String createConsumerTag(String s) {
                return s + UUID.randomUUID().toString();
            }
        });
        //监听容器
        /*
        simpleMessageListenerContainer.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                String msg = new String(message.getBody());
                System.out.println("ChannelAwareMessageListener监听消息："+msg);

            }
        });
        */
        /*

        //设置适配器，可以自定义处理类的方法名，接受参数类型，

        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter();
        messageListenerAdapter.setDelegate(new MessageDelegate());
        messageListenerAdapter.setDefaultListenerMethod("onMessage");
        messageListenerAdapter.setMessageConverter(new TextMessageConverter());

         */

        //Jackson2转换器
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new MessageDelegate());
        messageListenerAdapter.setDefaultListenerMethod("onJackson2Message");
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        messageListenerAdapter.setMessageConverter(jackson2JsonMessageConverter);


        //自定义队列对应的消息监听处理方法
        Map<String,String> queueAndMethodName = new HashMap<>();
        queueAndMethodName.put(RabbitMQConfig.DIRECT_QUEUE,MessageDelegate.QUEUE1_METHOD_NAME);
        queueAndMethodName.put(RabbitMQConfig.FANOUT_QUEUE,MessageDelegate.QUEUE2_METHOD_NAME);
        messageListenerAdapter.setQueueOrTagToMethodName(queueAndMethodName);

        simpleMessageListenerContainer.setMessageListener(messageListenerAdapter);




        simpleMessageListenerContainer.setConnectionFactory(connectionFactory);
        return simpleMessageListenerContainer;
    }
}
